using System.Security;
using System.Security.Cryptography;

namespace GEM.Domain.Entities.App;

public class AppRole : EntityBase
{
    public string RoleName { get; set; } = string.Empty;

    //数据权限范围：如"All","Department","Self"
    public string DataScope { get; set; } = "Self";

    //角色拥有的权限集合
    public ICollection<AppPermission> AppPermissions { get; set; } = new List<AppPermission>();

    //关联用户
    public ICollection<AppUser> AppUsers { get; set; } = new List<AppUser>();

    public bool AssignPermission(AppPermission Permission)
    {
        //如果不包含,则添加权限
        if (!AppPermissions.Contains(Permission))
        {
            AppPermissions.Add(Permission);
            return true;
        }
        return false;
    }

    public bool RemovePermission(AppPermission permission)
    {
        //如果包含指定权限则移除
        if (AppPermissions.Contains(permission))
        {
            AppPermissions.Remove(permission);
            return true;
        }
        return false;
    }

    //目前没用到
    public bool RemoveUsers(AppUser user)
    {
        if (AppUsers.Contains(user))
        {
            AppUsers.Remove(user);
            return true;
        }
        return false;
    }

    //判断当前角色是否拥有指定编码的权限
    public bool HasPermission(string permissionCode)
    {
        return AppPermissions.Any(p => p.Code == permissionCode);
    }

}